<template>
  <div class="juniorschoolSubjectstyle">
    <!-- 标题 -->
    <h2>{{ datas.text }}</h2>
    <!-- 提示 -->
    <p style="color: #969799; font-size: 12px; margin-top: 10px">
      拖动选中的导航可对其排序
    </p>
    <el-form label-width="80px" :model="datas" size="small" :rules="rules">
      <!-- 标题内容 -->
      <el-form-item class="lef" label="选择模板">
        <p style="color: #000; line-height: 4px">{{ styleText }}</p>
      </el-form-item>
      <!-- 卡片样式选择 -->
      <div class="commodityType">
        <el-tooltip v-for="(item, index) in commodityTypes" :key="index" class="item" effect="dark" :content="item.content" placement="bottom">
          <span class="iconfont" style="font-size: 21px" :class="[item.type === datas.commodityType ? 'active' : '', item.icon]" @click="onTest(index)" />
        </el-tooltip>
      </div>

      <div class="bor" />

      <!-- 应用列表 -->
      <div v-if="datas.imageList[0]">
        <vuedraggable v-model="datas.imageList" v-bind="dragOptions">
          <transition-group>
            <section v-for="(item, index) in datas.imageList" :key="item + index" class="imgList">
              <i class="el-icon-circle-close closeicon" @click="deleteimg(index)" />
              <!-- 背景图 -->
              <div class="backgroundImg" @click="showUpload('bgpic', index)">
                <img draggable="false" v-if="!item.bakcgroundImg" src="../../../../assets/images/backimg.png" alt="" />
                <img draggable="false" v-else :src="item.bakcgroundImg" alt="" />
                <p>上传背景</p>
              </div>
              <div v-for="(it, ind) in item.application" :key="ind" class="applist">
                <i class="el-icon-delete appcloseicon" @click="deleteApplication(index, ind)" />
                <!-- 图片 -->
                <div class="imag">
                  <img src="../../../../assets/images/add.png" @click="showUpload('icon', index, ind)" alt draggable="false" v-if="!it.src" />
                  <img :src="it.src" @click="showUpload('icon', index, ind)" alt="" draggable="false" />
                </div>
                <!-- 标题和链接 -->
                <div class="imgText">
                  <el-input v-model="it.text" placeholder="请输入应用名称" size="mini" clearable style="margin-bottom: 6px" />
                  <!-- 选择类型 -->
                  <select-link @selectThisLink="selectThisLink" :linktypeurl="it.url" :theIndex="ind" :sectionIndex="index" @clearThisLink="clearThisLink(index,ind)" />
                </div>
              </div>
              <el-button class="uploadImg" type="primary" plain @click="showUpload('icon', index)">
                <i class="el-icon-plus" />添加图文
              </el-button>
            </section>
          </transition-group>
        </vuedraggable>
      </div>

      <!-- 添加应用 -->
      <el-button class="uploadImg" type="primary" plain @click="addContent">
        <i class="el-icon-plus" />添加应用
      </el-button>
    </el-form>
    <!-- 上传图片 -->
    <uploadimg ref="upload" @uploadInformation="uploadInformation" />
  </div>
</template>
<script>
import uploadimg from "../../../uploadImg"; // 图片上传
import vuedraggable from "vuedraggable"; // 拖拽组件
import SelectLink from "../../../selectLink";
export default {
  name: "JuniorschoolSubjectstyle",
  components: { 
    uploadimg, 
    vuedraggable, 
    SelectLink 
  },
  props: {
    datas: Object,
  },
  data() {
    const kon = (rule, value, callback) => {
      if (value.length === 0) callback(new Error("请输入有效数字"));
    };
    return {
      dragOptions: {
        // 拖拽配置
        animation: 200,
      },
      emptyText: "",
      changeType: "",
      changeIndex: "",
      appIndex: "",
      commodityTypes: [
        {
          icon: "icon-yihangsange",
          type: 0,
          content: "一行三个",
        },
        {
          icon: "icon-yihangsige",
          type: 1,
          content: "一行四个",
        },
      ],
      rules: {
        textWeight: [{ required: true, validator: kon, trigger: "blur" }],
      },
      dangqian: "",
    };
  },
  computed: {
    styleText() {
      if (this.datas.commodityType === 0) return "一行三个";
      if (this.datas.commodityType === 1) return "一行四个";
    }
  },
  methods: {
    onTest(index) {
      this.datas.commodityType = index;
      this.$emit("updateView");
    },
    addContent() {
      this.datas.imageList.push({
        bakcgroundImg: "",
        application: [],
      });
    },
    showUpload(type, index, ind) {
      if (ind !== undefined) {
        this.appIndex = ind;
      }
      this.changeIndex = index;
      this.dangqian = type;
      this.$refs.upload.showUpload();
    },
    // 提交
    uploadInformation(res) {
      if (this.dangqian == "bgpic") {
        this.datas.imageList[this.changeIndex].bakcgroundImg = res;
      }
      if (this.dangqian == "icon") {
        if (this.appIndex === '') {
           this.datas.imageList[this.changeIndex].application.push({
            src: res,
            text: ''
           })
        } else {
          this.datas.imageList[this.changeIndex].application[this.appIndex].src = res;
        }
      }
    },
    // 选择链接
    selectThisLink(resu) {
      this.datas.imageList.forEach((it, ind) => {
        this.datas.imageList[ind].application.forEach((ite, inx) => {
          if (ind == resu.sectionIndex) {
            if (inx == resu.index) {
              this.datas.imageList[ind].application[inx].url = resu.url;
              this.datas.imageList[ind].application[inx].linktype = resu.linktype;
              this.datas.imageList[ind].application[inx].id = resu.id;
            }
          }
        });
      });
    },
    clearThisLink (index,ind) {
      this.datas.imageList[index].application[ind].url = '';
    },
    /* 删除应用区 */
    deleteimg(index) {
      this.datas.imageList.splice(index, 1);
    },
    /* 删除应用模块 */
    deleteApplication(index,ind) {
      this.datas.imageList[index].application.splice(ind, 1);
    },
  },
};
</script>
<style lang="scss" scoped>
  .juniorschoolSubjectstyle{
    width: 100%;
    position: absolute;
    left: 0;
    top: 0;
    padding: 0 10px 20px;
    box-sizing: border-box;
    .bor {
      height: 1px;
      margin: 20px 0;
      background-color: #ebedf0;
    }
    /* 标题 */
    h2 {
      padding: 24px 16px 24px 0;
      margin-bottom: 15px;
      border-bottom: 1px solid #f2f4f6;
      font-size: 18px;
      font-weight: 600;
      color: #323233;
    }

    .lef {
      .el-form-item__label {
        text-align: left;
      }
    }

    /* 上传图片按钮 */
    .uploadImg {
      width: 303px;
      height: 40px;
      margin-top: 10px;
    }

    /* 列表 */
    .imgList {
      padding: 6px 12px;
      margin: 16px 7px;
      border-radius: 2px;
      background-color: #fff;
      box-shadow: 0 0 4px 0 rgba(10, 42, 97, 0.2);
      position: relative;

      /* 删除图标 */
      .closeicon {
        position: absolute;
        right: -10px;
        top: -10px;
        cursor: pointer;
        font-size: 19px;
      }
      .applist {
        display: flex;
        align-items: center;
        margin-top: 10px;
        position: relative;

        .appcloseicon {
          position: absolute;
          right: -23px;
          top: 20px;
          cursor: pointer;
          font-size: 19px;
        }
        /* 图片 */
        .imag {
          width: 60px;
          height: 60px;
          border-radius: 5px;
          overflow: hidden;
          position: relative;
          cursor: pointer;
          img {
            width: 100%;
            height: 100%;
            display: inline-block;
          }
          span {
            background: rgba(0, 0, 0, 0.5);
            font-size: 12px;
            position: absolute;
            left: 0px;
            bottom: 0px;
            display: inline-block;
            width: 100%;
            text-align: center;
            color: #fff;
            height: 20px;
            line-height: 20px;
          }
        }

        /* 图片字 */
        .imgText {
          width: 80%;
          display: flex;
          flex-direction: column;
          box-sizing: border-box;
          padding-left: 20px;
          justify-content: space-between;
          .select-type {
            display: flex;
            .el-select {
              .el-input {
                padding: 5px 0;
                input {
                  white-space: nowrap;
                  overflow: hidden;
                  text-overflow: ellipsis;
                }
              }
            }
          }
        }

        .isub {
          padding: 5px 0;
          span {
            font-size: 14px;
            color: #666666;
          }
        }
      }

      /* 背景图 */
      .backgroundImg {
        display: inline-flex;
        align-items: center;
        cursor: pointer;
        width: 60px;
        height: 60px;
        position: relative;
        background: #f2f4f6;

        img {
          width: 100%;
          height: auto;
        }

        p {
          position: absolute;
          left: 0;
          bottom: 0;
          width: 100%;
          height: 20px;
          font-size: 12px;
          color: #fff;
          background: rgba(0, 0, 0, 0.5);
          text-align: center;
          line-height: 20px;
        }
      }

    }

    /* 列表样式 */
    .commodityType {
      display: flex;
      justify-content: space-around;
      align-items: center;
      span {
        display: inline-block;
        width: 58px;
        height: 32px;
        text-align: center;
        line-height: 32px;
        background: #fff;
        border: 1px solid #ebedf0;
        color: #979797;
        margin: 0 1px;
        cursor: pointer;
        transition: all 0.5s;

        &:hover {
          border: 1px solid #155bd4;
          color: #155bd4;
        }

        &.active {
          border: 1px solid #155bd4;
          background-color: #e0edff;
          color: #155bd4;
        }
      }
    }

  }
</style>